<?php
namespace Home\Controller;
use Think\Controller;
//set_time_limit(100);

class LoginController extends BaseController {
    public $obj;
    public function __construct(){
        parent::__construct();
        //$this->obj = A('WeixinWeb','Event');
        $this->obj = new \Home\Event\LetterEvent();
    }

    public function index(){
        //$url = I('get.url','');
        //$this->assign('url',$url);
        $controller = I('get.controller','');
        $action = I('get.action','');
        $local_id = I('get.local_id','');
//        dump($controller);
//        dump($action);
//        dump($local_id);die;
        $this->assign('controller',$controller);
        $this->assign('action',$action);
        $this->assign('local_id',$local_id);
        $this->display('Login/login');
    }

    /**
     * 登陆
     */
    public function loginUser(){
        $ildMobile = session('verifyMobile');
        $code = I('code') or $this->ajaxReturn(['status' => false,'msg' => '请输入验证码！']);
        $mobile = I('mobile') or $this->ajaxReturn( ['status' => false,'msg' => '请输入手机号！']);
        //$url = I('url') ? I('url') : 'Member/index' ;
        $controller = I('post.controller','');
        $action = I('post.action','');
        $local_id = I('post.local_id','');
        if(empty($controller) || empty($action) ){
            $url = 'Member/index' ;
        }else{
            $url = $controller.'/'.$action.'/local_id/'.$local_id;
        }

        $openid = session('wx_openid');
        if($mobile != $ildMobile) {
            $this->ajaxReturn( ['status' => false,'msg' => '您输入的手机不匹配！']);
        }
        $stat = $this->obj->verifyMobile($code);
        if($stat['stat'] == false){
            $this->ajaxReturn( ['status' => false,'msg' => $stat['msg']]);
        }
        //查询数据库登陆
        //$userInfo = M('b2c_members')->where(['mobile' => $mobile])->find();
        $userInfo = M('b2c_members')->where(['mobile' => $mobile,'openid' => $openid])->find();

        //echo M()->_sql();die;
        if($userInfo){
            session('userInfo',$userInfo);
            $this->ajaxReturn(['status' => true,'msg' => '登陆成功！','url'=>U($url)]);
        }

        //判断手机号是否已被其他账号使用
        if(M('b2c_members')->where(['mobile' => $mobile,'openid' =>['neq',$openid] ])->find()){
            $this->ajaxReturn( ['status' => false,'msg' => '该手机号已被他人使用！']);
        }


        //$this->ajaxReturn(['status' => false,'msg' => $openid]);
        if(!$openid){
            $this->ajaxReturn(['status' => false,'msg' => '登陆失败！']);
        }
        //$this->ajaxReturn(['status' => false,'msg' => U($url)]);
        $temp_id = M("b2c_temp_user")->where(['openid' => $openid])->save(['mobile' => $mobile,'authentication' => '1']);
        if($temp_id === false){
            $this->ajaxReturn(['status' => false,'msg' => '登陆失败！']);
        }
        $t_userInfo = M("b2c_temp_user")->where(['openid' => $openid])->find();
        $members_data = [
            'member_lv_id' => 1,
            'status' => '1',
            'point' => 0,
            'mobile' => $mobile,
            'sex' => '2',
            'advance' => 0.00,
            'advance_freeze' => 0.00,
            'reg_ip' => get_client_ip(),
            'regtime' => time(),
        ];
        if($t_userInfo){
            $members_data['default_local_id'] = $t_userInfo['default_local_id'] ? $t_userInfo['default_local_id'] : 0;
            $members_data['nickname'] = $t_userInfo['nickname'];
//            $members_data['openid'] = $t_userInfo['openid'];
           //查询members 是否有数据，没有就add
           $m_data = M('b2c_members')->where(['openid'=>$t_userInfo['openid']])->find();
           if(empty($m_data)){
                //添加members 数据
               $add_data = [
                   'openid' => $t_userInfo['openid'],
                   'nickname' => $t_userInfo['nickname'],
                   'default_local_id' => $t_userInfo['default_local_id'] ? $t_userInfo['default_local_id'] : 0,
                   'status' => '1',
                   'mobile' => $t_userInfo['mobile'],
                   'sex' => '2',
                   'advance' => 0.00,
                   'advance_freeze' => 0.00,
                   'reg_ip' => get_client_ip(),
                   'regtime' => $t_userInfo['regtime'],
               ];
              $add_members = M('b2c_members')->add($add_data);
               if($add_members === false){
                   $this->ajaxReturn(['status' => false,'msg' => '网络异常，请稍候再试！']);
               }
           }

        }
        $members_id = M("b2c_members")->where(['openid' => $t_userInfo['openid']])->save($members_data);
        if($members_id === false){
            $this->ajaxReturn(['status' => false,'msg' => '登陆失败！']);
        }

        //$userInfo = M('b2c_members')->where(['mobile' => $mobile])->find();
        $userInfo = M('b2c_members')->where(['mobile' => $mobile,'openid' => $openid])->find();
        session('userInfo',$userInfo);
        $this->ajaxReturn(['status' => true,'msg' => '登陆成功！','url'=>U($url)]);
    }

    /**
     * 改绑手机
     */
    public function alterMobileOne(){
        $member_data = session('userInfo');
        if(!$member_data){
            $this->error('还未登录，现带你去登录...',U('Login/index'));
        }
        $mobile = $member_data['mobile'];

        $this->assign('mobile',$mobile);
        $this->display('Login/alterMobile');
    }

    public function alterMobile(){
        $member_data = session('userInfo') or $this->ajaxReturn( ['status' => false,'msg' => '还未登录，现带你去登录...','url' => U('Login/index')]);
        $code = I('code') or $this->ajaxReturn( ['status' => false,'msg' => '验证失败！']);
        $type = I('type') or $this->ajaxReturn( ['status' => false,'msg' => '验证失败！']);
        $ildMobile = $member_data['mobile'];
        //1、验证原手机是否在当时会员身上
        if($type == 1){
            $mobile = I('mobile') or $this->ajaxReturn( ['status' => false,'msg' => '请输入原手机号！']);
            if($mobile != $ildMobile) {
                $this->ajaxReturn( ['status' => false,'msg' => '您输入的手机跟原来的不匹配！']);
            }
            $stat = $this->obj->verifyMobile($code);
            if($stat['stat'] == false){
                $this->ajaxReturn( ['status' => false,'msg' => $stat['msg']]);
            }
            $this->ajaxReturn( ['status' => true,'msg' => '验证成功']);
        }
        //2、改绑手机号
        $mobile = I('mobile') or $this->ajaxReturn( ['status' => false,'msg' => '请输入新手机号！']);
        //发送验证码的手机
        $mobile2 = session('verifyMobile');
        if($mobile != $mobile2) {
            $this->ajaxReturn( ['status' => false,'msg' => '未知错误！']);
        }
        //验证新手机号是否被他人使用
        if(M('b2c_members')->where(['mobile' => $mobile])->find()){
            $this->ajaxReturn( ['status' => false,'msg' => '该手机号已被他人使用！']);
        }

        $code = I('code') or $this->ajaxReturn( ['status' => false,'msg' => '验证失败！']);
        $stat = $this->obj->verifyMobile($code);
        if($stat['stat'] == false){
            $this->ajaxReturn( ['status' => false,'msg' => $stat['msg']]);
        }
        $model = M();
        try{
            $model->execute('begin');
            $userStat = M('b2c_members')->where(['openid' => $member_data['openid']])->save(['mobile' => $mobile]);
            if($userStat === false){
                throw new \Exception(false);
            }
            $userInfo = M('b2c_members')->where(['mobile' => $mobile])->find();

            $t_userStat = M('b2c_temp_user')->where(['openid' => $userInfo['openid']])->save(['mobile' => $mobile]);
            if($t_userStat === false){
                throw new \Exception(false);
            }
            $t_userInfo = M('b2c_temp_user')->where(['openid' => $userInfo['openid']])->find();

            session('t_userInfo',$t_userInfo);
            session('userInfo',$userInfo);
            // 提交事务
            $model->execute('commit');
            $this->ajaxReturn( ['status' => true,'msg' => '改绑手机号成功...','url' => url('Member/index')]);

        }catch (\Exception $e){
            // 事务回滚
            $model->execute('rollback');
            $this->ajaxReturn( ['status' => false,'msg' => '改绑手机号失败！']);
        }
    }



    /*public function alterMobileTwo(){

        $code = I('code') or $this->ajaxReturn( ['status' => false,'msg' => '验证失败！']);
        $stat = $this->obj->verifyMobile($code);
        if($stat['stat'] == false){
            $this->ajaxReturn( ['status' => false,'msg' => $stat['msg']]);
        }
        $this->ajaxReturn( ['status' => false,'msg' => '验证成功']);
    }*/
    /*验证验证码，修改手机号*/
    /*public function alterMobile(){
        $member_data = session('userInfo');
        if(!$member_data){
            $this->ajaxReturn( ['status' => false,'msg' => '还未登录，现带你去登录...','url' => url('Login/index')]);
        }
        $ildMobile = $member_data['mobile'];
        $mobile = session('verifyMobile');
        $code = I('code') or $this->ajaxReturn( ['status' => false,'msg' => '验证失败！']);
        $stat = $this->obj->verifyMobile($code);
        if($stat['stat'] == false){
            $this->ajaxReturn( ['status' => false,'msg' => $stat['msg']]);
        }
        $model = M();
        try{
            $model->execute('begin');
            $userStat = M('b2c_members')->where(['mobile' => $ildMobile])->save(['mobile' => $mobile]);
            if($userStat === false){
                throw new \Exception(false);
            }
            $t_userStat = M('b2c_temp_user')->where(['mobile' => $ildMobile])->save(['mobile' => $mobile]);
            if($t_userStat === false){
                throw new \Exception(false);
            }
            $userInfo = M('b2c_members')->where(['mobile' => $ildMobile])->find();
            session('userInfo',$userInfo);
            // 提交事务
            $model->execute('commit');
            $this->ajaxReturn( ['status' => true,'msg' => '改绑手机号成功...','url' => url('Member/index')]);

        }catch (\Exception $e){
            // 事务回滚
            $model->execute('rollback');
            $this->ajaxReturn( ['status' => false,'msg' => '改绑手机号失败！']);
        }

    }*/


    public function __destruct(){
        parent::__destruct();
        unset($this->obj);
    }
    

   

}